/**
 * 
 */
package com.geezdata.cps.ttk.core.command;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.geezdata.cps.base.response.BaseResponse;
import com.geezdata.cps.base.utils.BeanUtil;
import com.geezdata.cps.cqrs.command.Command;
import com.geezdata.cps.sdk3rd.pinduoduo.domain.PinduoduoDDKGoodsPromotionUrl;
import com.geezdata.cps.sdk3rd.pinduoduo.domain.PinduoduoDDKGoodsPromotionWeAppInfo;
import com.geezdata.cps.ttk.common.request.CreateOrUpdatePromotionUrlRequest;
import com.geezdata.cps.ttk.common.response.CreateOrUpdatePromotionUrlResult;
import com.geezdata.cps.ttk.common.vo.PromotionUrlResVO;
import com.geezdata.cps.ttk.core.util.PromotionSpaceManage;
import com.geezdata.cps.ttk.domain.domain.PromotionUrl;
import com.geezdata.cps.ttk.domain.domain.Sharer;
import com.geezdata.cps.ttk.domain.domain.WeAppInfo;
import com.geezdata.cps.ttk.integration.ddk.DdkIntegrationService;
import com.geezdata.cps.ttk.mapper.mapper.PromotionUrlMapper;
import com.geezdata.cps.ttk.mapper.mapper.WeAppInfoMapper;
import com.geezdata.cps.ttk.query.query.SharerService;

/**
 * @author tfwan
 * @date 创建时间：2018年6月12日 下午3:45:16
 *
 */
@Service
public class CreateOrUpdatePromotionUrlCommand implements Command<CreateOrUpdatePromotionUrlRequest, CreateOrUpdatePromotionUrlResult> {
	@Autowired
	private SharerService sharerService;
	@Autowired
	private PromotionSpaceManage promotionSpaceManage;
	@Autowired
	private DdkIntegrationService ddkIntegrationService;
	@Autowired
	private PromotionUrlMapper promotionUrlMapper;
	@Autowired
	private WeAppInfoMapper weAppInfoMapper;
	
	@Override
	@Transactional
	public BaseResponse<CreateOrUpdatePromotionUrlResult> execute(CreateOrUpdatePromotionUrlRequest request) {
		CreateOrUpdatePromotionUrlResult result = new CreateOrUpdatePromotionUrlResult();
		List<PromotionUrlResVO> promotionUrlResVOList = new ArrayList<PromotionUrlResVO>();
	    Sharer sharer = sharerService.getSharerByOpenId(request.getOpenId());
		String pId = promotionSpaceManage.getOrCreatePromotionSpace(sharer).getPlatformPromotionSpaceId();
		String customParameters = "task_id=" + request.getTaskId() + "&open_id=" + request.getOpenId();
		PinduoduoDDKGoodsPromotionUrl queryPromotionUrl = ddkIntegrationService.getPromotionUrl(pId, request.getGoodsId(), customParameters , true);

        createPromotionUrl(queryPromotionUrl, sharer, request.getGoodsId());
        result.setPromotionUrlList(promotionUrlResVOList);
		return BaseResponse.success(result);
	}

	private void createPromotionUrl(PinduoduoDDKGoodsPromotionUrl ddkGoodsPromotionUrl, Sharer sharer, String goodsId) {
		PromotionUrl promotionUrl = convent2PromotionUrl(ddkGoodsPromotionUrl, sharer, goodsId);
		promotionUrlMapper.insert(promotionUrl);
		
		WeAppInfo weAppInfo = convent2WebAppInfo(ddkGoodsPromotionUrl.getWeAppInfo(), promotionUrl.getPromotionUrlId());
		weAppInfoMapper.insert(weAppInfo);
	}

	private WeAppInfo convent2WebAppInfo(PinduoduoDDKGoodsPromotionWeAppInfo ddkWeAppInfo, Long promotionUrlId) {
		WeAppInfo weAppInfo = BeanUtil.convert(ddkWeAppInfo, WeAppInfo.class);
		weAppInfo.setPromotionUrlId(promotionUrlId);
		weAppInfo.setStatus(1);
		Date now = new Date();
		weAppInfo.setCreatedAt(now );
		weAppInfo.setUpdatedAt(now);
		return weAppInfo;
	}

	private PromotionUrl convent2PromotionUrl(PinduoduoDDKGoodsPromotionUrl ddkGoodsPromotionUrl, Sharer sharer, String goodsId) {
		PromotionUrl promotionUrl = new PromotionUrl();
		promotionUrl.setMobileShortUrl(ddkGoodsPromotionUrl.getMobileShortUrl());
		promotionUrl.setMobileUrl(ddkGoodsPromotionUrl.getMobileUrl());
		promotionUrl.setShortUrl(ddkGoodsPromotionUrl.getShortUrl());
		promotionUrl.setUrl(ddkGoodsPromotionUrl.getShortUrl());
		promotionUrl.setOpenId(sharer.getOpenId());
		promotionUrl.setGoodsId(goodsId);
		promotionUrl.setStatus(1);
		Date now = new Date();
		promotionUrl.setCreatedAt(now );
		promotionUrl.setUpdatedAt(now);
		return promotionUrl;
	}
}
